var Department = function(){



	var thiz=this;
	var departStore = new Ext.data.JsonStore({
			url : '../UserManagement/QueryDepart2.form?timesnap=' + new Date().getTime(),
			root : 'result',
				
				fields : ['DEPARTMENT_ID', 'DEPARTMENT_NAME','DOWNDEPART_NAME',
				  'DOWNDEPART_ID','parent_id'],
				
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						//permissionAdd.items.itemAt(0).setValue(true);
						thiz.baseParams = {
						start : 0,
						limit : 9999
						
						};
					}
					
				}
			});

	var tempvar = new Ext.form.TextField({
		fieldLabel: 'Temp',
		width : 200,
		name:'temp',
		allowBlank: false
	});


	var addDepartName = new Ext.form.TextField({
		fieldLabel: '部门名称',
		width : 200,
		name:'addDepartName',
		allowBlank: false
	});
	
	var addUpDepartName = new Ext.form.ComboBox( {
		fieldLabel : '上级部门',
		//hiddenName : 'type',
		store: departStore,//createInfoStore  departStore
		mode : 'local',
		displayField : 'DEPARTMENT_NAME',
		triggerAction : 'all',
		editable : false,
		valueField : 'DEPARTMENT_ID',
		typeAhead : true,
		width : 200,
		value:1,
		selectOnFocus : true,
		selectOnChange : true
		
	});
	
	
	
	var addDepartNum = new Ext.form.TextField({
		fieldLabel: '部门编号',
		width : 200,
		name:'addDepartNum',
		allowBlank: false
	});
	
	var pass="";
	
	
	
	
	var Email = new Ext.form.TextField({
		fieldLabel: 'E-mail',
		width : 200,
		name:'email',
		allowBlank: true
	});
	
	//查找
	var SearchDeartName = new Ext.form.TextField({
	
		fieldLabel: '部门名称',
		width : 200,
		name:'DepartSearchName',
		allowBlank: true
	});
	
	var SearchDepartNum = new Ext.form.TextField({
		fieldLabel: '部门编号',
		width : 200,
		name:'Departnum',
		allowBlank: true
	});
	
	
	
	//修改
	var EditDepartName = new Ext.form.TextField({
		fieldLabel: '部门名称',
		width : 200,
		name:'username',
		allowBlank: false
	});
	
	var EditDepartNum = new Ext.form.ComboBox( {
		fieldLabel : '上级部门',
		//hiddenName : 'type',
		store: departStore,
		mode : 'local',
		displayField : 'DEPARTMENT_NAME',
		triggerAction : 'all',
		editable : false,
		valueField : 'DEPARTMENT_ID',
		typeAhead : true,
		width : 200,
		value:1,
		selectOnFocus : true,
		selectOnChange : true
	});
	var EditId = new Ext.form.TextField({
		fieldLabel: '部门编号Id',
		width : 200,
		name:'userid',
		allowBlank: false,
		disabled:true
	});
	
	
	//删除
	
	
	
	var addDepartDetail = new Ext.form.FormPanel({
		el:'form',
		labelAlign:'left',
		labwidth:50,
		frame:true,
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[
			      		 {columnWidth:3,items:[addDepartName]},
			      		 {columnWidth:3,items:[addUpDepartName]}
			      		 ]
			      }]
	});
	
	createDepartStore= new Ext.data.JsonStore({
		url : '../UserManagement/addDepart.form?timesnap=' + new Date().getTime(),
		
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = {
						departName : addDepartName.getValue(),
						UpDepartName : addUpDepartName.getValue()
						};
					}
				}
			});
	
	var doCreate = function(dp){
		var departName=addDepartName.getValue();
		if(departName == ''){
			Utils.warn2('请录入部门名称！',180);
		}
		else{
			Utils.confirm2({
				msg:"是否增加？",
				width:220,
				fn:function(){
						createDepartStore.load();
						Utils.info("保存成功","系统提示");	
						createInfoStore.load();
						
						//departStore.load();
						//departStore.on('load',function(tempstore){alert(tempstore.getCount())});
						
							}
						})
					}
				}
	
	var addDepart = new Ext.Window({
		el:'addDepart',
		layout:'fit',
		title: '添加部门',
        width: 400,
        height:300,
         closeAction: 'hide',
        items:[addDepartDetail],
         buttons:[{text:'确认',icon: '../resources/icons/accept.png',scope:this,handler:function(){
         			doCreate(this.addDepart);}
         			},
		         {text:'清空',icon: '../resources/icons/arrow_undo.png',scope:this,handler:function(){
		         	}}]
});


	var search = new Ext.form.FormPanel({
		el:'search',
		labelAlign:'left',
		labwidth:50,
		frame:true,
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[
			      		 {columnWidth:3,items:[SearchDeartName]},
			      		  {columnWidth:3,items:[SearchDepartNum]}
			      		 ]
			      		 }]
			      		 
	});
	

	function Search(){

	}
	
	var delInfoStore = new Ext.data.JsonStore(
			{url : '../UserManagement/DelUser.form?timesnap=' + new Date().getTime(),
				root : 'userInfo',
				fields : ['userId', 'id', 'userName',
						'userDesc', 'userAdmin', 'roleId',
						'branchId', 'createUser', 'createDate', 
						'isActive','beginFlag', 'endFlag', 'departmentId', 'email',
						'phone', 'gender', 'age'],			
						
						
						
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = {
						userName:tempvar.getValue()
						};
					}
				}
			});
	
	
	var saveTract = function(bs){

	}
	var createInfoStore = new Ext.data.SimpleStore({
		fields : ['DEPARTMENT_ID', 'DEPARTMENT_NAME','DOWNDEPART_NAME',
				  'DOWNDEPART_ID','parent_id']
	});
	
	
	
	
	var searchDepart = new Ext.Window({
		el:'searchDepart',
		layout:'fit',
		title: '查询部门',
        width: 400,
        height:300,
         closeAction: 'hide',
        items:[search],
         buttons:[{text:'确认',icon: '../resources/icons/accept.png',scope:this,
         		   handler:function(){
         		   //createInfoStore.load();
         		   		var url='../UserManagement/QueryDepart.form?timesnap=' + Date.parse(new Date());
         		   		alert(SearchDeartName.getValue());
         		   		alert(SearchDepartNum.getValue());
						var param = {
						departName : SearchDeartName.getValue(),
						departNum : SearchDepartNum.getValue()
						};
			    		scope=this;
						search.getForm().submit({
							waitTitle : '系统提示',
							waitMsg : '数据提交中，请稍候...',
							url : url,
							params:param,
							method : 'POST',			
							success : function(fp, o){
								createInfoStore.loadData(o.result.result,false);

							},
							failure : function(fp, o){
								alert('error');
							}
						});		
         		   }
         	
         	},
         
         {text:'清空',icon: '../resources/icons/arrow_undo.png',scope:this,handler:function(){
         	}}
         ]
	});

	var edit = new Ext.form.FormPanel({
		el:'edit',
		labelAlign:'left',
		labwidth:50,
		frame:true,
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[{columnWidth:3,items:[EditDepartName]},
			      		 {columnWidth:3,items:[EditDepartNum]},
			      		 {columnWidth:3,items:[EditId]}
			      		 ]
			      		 }]
	});
	var xg = Ext.grid;
	var sm = new Ext.grid.CheckboxSelectionModel();
	var createGrid = new Ext.grid.GridPanel({
	sm: sm,
		width : 930,
		height : 300,
		store : createInfoStore,
		scope : this,
		listeners : {
			scope : this,
			rowmousedown : function(grid,index,e){
				var data = grid.store.getAt(index).data;
			}
		},
		columns : [
		sm,
			{header:'上级部门ID',sortable: true,width: 50,dataIndex:'DEPARTMENT_ID'},
			{header:'上级部门',sortable: true,width: 150,dataIndex:'DEPARTMENT_NAME'},
		    {header:'部门名称',sortable: true,width: 100, dataIndex:'DOWNDEPART_NAME'},
		    {header:'部门ID',sortable: true,width: 100, dataIndex:'DOWNDEPART_ID'}
		    ]
		           

	});
	
	var agree = new Ext.form.Radio( {
		boxLabel : '是',
		name : 'status',
		checked:true,
		inputValue : 11
	});

	var unAgree = new Ext.form.Radio( {
		boxLabel : '否',
		name : 'status',
		inputValue : 12
	});
	
	var isAgree = new Ext.form.RadioGroup({
		fieldLabel : '是否签约',
		columns : 2,
		items : [agree,unAgree]
	});
	
	var remark = new Ext.form.TextArea({
		fieldLabel : '合同细节',
		width : 840,
		height:200
	});

	var updateDepart = new Ext.data.Connection({
		waitTitle : '系统提示',
		waitMsg : '数据提交中，请稍候...',
		url:'../UserManagement/updateDepart.form?timesnap=' + new Date().getTime(),
		method:'post'
	});

	var editDepart = new Ext.Window({
		el:'editDepart',
		layout:'fit',
		title: '修改部门',
        width: 400,
        height:300,
         closeAction: 'hide',
        items:[edit],
         buttons:[{text:'确认',icon: '../resources/icons/accept.png',scope:this,
         		handler:function(){ 
         			Utils.confirm2({
					msg:"是否更改数据信息？",
					width:220,
						fn:function(){
							Utils.request(updateDepart,{
							params:{
								EditDepartName: EditDepartName.getValue(),//部门名称
								EditDepartNum: EditDepartNum.getValue(),//上级部门ID
								id: EditId.getValue()//部门ID
							},
							success:function(data){
								Utils.info("保存成功","系统提示");	
								createInfoStore.load();
							}
						  })			
						}
					})
         		}
         	},
         
         {text:'清空',icon: '../resources/icons/arrow_undo.png',scope:this,handler:function(){
         	}}
         ]
	});
	

this.CreateDepart = new Ext.form.FormPanel({

	frame : true,
		width : 800,
		fileUpload: true, 
		labelWidth : 85,
		buttons : [
		           {text:'新增',icon: '../resources/icons/add.png',scope:this,handler:function()
		           		{addDepart.show();	}
		           	},
		           {text:'查询',icon: '../resources/icons/find.png',scope:this,handler:function()
		           		{searchDepart.show();	}
		           	},
		           	{text:'修改',icon: '../resources/icons/application_edit.png',scope:this,
		           		handler:function(){
		           			var count=createGrid.getSelectionModel().getSelections();
		           			if(count.length==0){
		           				Utils.warn2('请选择一条记录！',180);
								return ;
		           			}else if(count.length>1){
								Utils.warn2('只能选择一条记录！',180);
								return;}
								EditDepartName.setValue(count[0].data.DOWNDEPART_NAME);//部门名称
								EditDepartNum.setValue(count[0].data.DEPARTMENT_ID);//上级部门ID
								EditId.setValue(count[0].data.DOWNDEPART_ID)//部门ID
								editDepart.show();
					
						}		           		
		           	}
		           ],
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[]
				},createGrid],
	   listeners: {
	    	render: {fn:function(){ 
	    		var url='../UserManagement/QueryDepart.form?timesnap=' + Date.parse(new Date());
				var param;
	    		scope=this;
				search.getForm().submit({
					waitTitle : '系统提示',
					waitMsg : '数据提交中，请稍候...',
					url : url,
					params:param,
					method : 'POST',			
					success : function(fp, o){
						createInfoStore.loadData(o.result.result,false);
						departStore.load();
						departStore.on('load',function(tempstore){
							//alert(tempstore.getCount())
						});
						
					//	departStore.loadData(o.result.result,false);
					},
					failure : function(fp, o){
						alert('error');
					}
				});		
				//createInfoStore.load();
				//createInfoStore.on('load',function(tempstore){alert(tempstore.getCount())});
				departStore.load();
				//departStore.on('load',function(tempstore){
					//alert(tempstore);
					//alert("ddd");
					//alert(tempstore.getCount());
					
					//});
			}}
		}
	});
	//确立查询人员
		var  conn = new Ext.data.Connection({
        method: 'POST',
        url: '../UserManagement/QueryUser.form?timesnap=' + Date.parse(new Date())
    });
	
	
	
	
	
	
	
	//确定立项信息路径
	var addCon = new Ext.data.Connection({
		url:'../project/doCreateProject.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	
};